$(document).ready(function(){
	$('#submit').click(function(){
		$('form').submit();
	});
	
	$('#info_movie').click(function() {
		
		id_tmdb =  $('input[name="id_tmdb"]:checked').val();
		
		if(id_tmdb != undefined){
			ajax_get_info_movie(id_tmdb);
		}
		else{
			alert('Seleccione una Pelicula de la Lista');
		}
		
	});
	
	
});



function ajax_get_info_movie(id_tmdb){
	$.ajax({
		type: "POST",
		url: base_url+lang_url+'panel/films/get_info_movie',
		data: 'id_tmdb='+id_tmdb,
		dataType: 'json',
		success: function(rsp){
//			alert(rsp);
			if(rsp.hasOwnProperty('error')==false){
				
				window.parent.$('#title').val(rsp.title);
				window.parent.$('#poster').val(rsp.poster);
				window.parent.$('#date_release').val(rsp.date_release);
				window.parent.$('#synopsis_en').val(rsp.synopsis_en);
				
				trailer = rsp.trailers.split("=");
				window.parent.$('#trailers').val(trailer[1]);
				
				window.parent.$('#languaje').val('Ingles');
				window.parent.$('#subtitle').val('Español');
				window.parent.$('#format_film').val('MP4');
				
				// CAST
				//		<label>
				//			<input type="checkbox" name="cast[]" value=""/>
				//			porta ut cras odio magna? Cras augue arcu a aliquet sit phasellus
				//		</label>

				var cast = [];
				var t = 0;
				
				for(var i in rsp.cast){
					for(var x in rsp.cast){
						if(rsp.cast[i].id == rsp.cast[x].id){
							t = t + 1;
						}
					}
					if(t==1){
						cast.push('<label>', '<input type="checkbox" name="cast[]" value="', rsp.cast[i].id ,'" checked/>', rsp.cast[i].name, '</label>');
					}
					
					if(i==10){break;}
					t = 0;
				}
				var html_cast = cast.join('');
				window.parent.$('#div-cast').empty();
				window.parent.$('#div-cast').append(html_cast);
					
					
				// CATEGORIES
				//	<label>
				//		<input type="checkbox" name="categories[]" value=""/>
				//		porta ut cras odio magna? 
				//	</label>
					
				var categories = [];
				categories.push('<label>', '<input type="checkbox" name="categories[]" value="48"/>R5</label>');
				categories.push('<label>', '<input type="checkbox" name="categories[]" value="50"/>HDRip</label>');
				categories.push('<label>', '<input type="checkbox" name="categories[]" value="49"/>DVDScreener</label>');
				categories.push('<label>', '<input type="checkbox" name="categories[]" value="1"/>DVDRip</label>');
				categories.push('<label>', '<input type="checkbox" name="categories[]" value="2"/>BRRip</label>');
				categories.push('<label>', '<input type="checkbox" name="categories[]" value="51"/>BRRip 480p</label>');
				categories.push('<label>', '<input type="checkbox" name="categories[]" value="3"/>BRRip 720p</label>');
				for(var i in rsp.categories){
					categories.push('<label>', '<input type="checkbox" name="categories[]" value="', rsp.categories[i].id ,'" checked/>', rsp.categories[i].name, '</label>');
				}
				var html_categories = categories.join('');
				window.parent.$('#div-categories').empty();
				window.parent.$('#div-categories').append(html_categories);				
				
				window.parent.$("p.close a").click();
			}
			else{
				alert(rsp.error);
			}
			
		},
		error: function(e){
			alert('Error:' + e);
		}
	});
}